package com.lancoo.znbkxx.teacher.ui.activity

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lancoo.cpbase.authentication.base.CurrentUser
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.adapter.HomeWorkAdapter
import com.lancoo.znbkxx.teacher.adapter.SpacesItemDecoration
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPersonalAnalysis.AibkAnalysisAnswerSheetActivity_1v1
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPreview.AibkPaperPreviewActivity
import com.lancoo.znbkxx.teacher.aibkTestCoach.questionToMark.AibkQuesMarkActivity
import com.lancoo.znbkxx.teacher.base.BaseVMActivity
import com.lancoo.znbkxx.teacher.databinding.ActivityHomeworkBinding
import com.lancoo.znbkxx.teacher.model.data.PaperBaseInfo
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import com.lancoo.znbkxx.teacher.viewmodel.PaperViewModel

import kotlinx.android.synthetic.main.activity_homework.*

/**
 * @author : wealive
 * @date   : 2020/10/9 14:03
 * @desc   :
 */
class HomeWorkActivity : BaseVMActivity<PaperViewModel, ActivityHomeworkBinding>(),
        BaseQuickAdapter.OnItemChildClickListener {
    private val stuName: String by lazy { intent.getStringExtra("stuName") }
    private val stuID: String by lazy { intent.getStringExtra("stuID") }
    val list = mutableListOf<PaperBaseInfo>()
    private var pageSize = 10
    private var totalCount = 0
    lateinit var adapter: HomeWorkAdapter
    override fun providerVMClass(): Class<PaperViewModel>? {
        return PaperViewModel::class.java
    }

    override fun setLayoutID(): Int {
        return R.layout.activity_homework
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        tv_title.text = "${stuName}的作业情况"
        sbtn_back.setOnClickListener { finish() }
        val emptyView = layoutInflater.inflate(R.layout.item_empty, null)
        adapter = HomeWorkAdapter(list)
        adapter.emptyView = emptyView
        adapter.onItemChildClickListener = this
        val manager = LinearLayoutManager(this).apply { orientation = LinearLayoutManager.VERTICAL }
        val map = hashMapOf<String, Int>()
        map[SpacesItemDecoration.TOP_SPACE] = DensityUtils.dp2px(this, 10f)
        val itemDecoration = SpacesItemDecoration(map)
        rv.addItemDecoration(itemDecoration)
        rv.layoutManager = manager
        rv.adapter = adapter
        swipe_layout.setOnRefreshListener {
            pageSize = 10
            initPaper()
        }
        adapter.setOnLoadMoreListener({
            if (totalCount >= pageSize) {
                pageSize += 10
                initPaper()
            } else {
                adapter.loadMoreEnd()
            }
        }, rv)
    }

    private fun initPaper() {
        viewModel.getPaperBaseInfoList(
                CurrentUser.Token,
                CurrentUser.UserID,
                stuID,
                CurrentUser.SchoolID,
                -1,
                1,
                pageSize,
                1
        )
    }

    override fun startObserve() {
        viewModel.mPaperBaseInfoList.observe(this, Observer {
            totalCount = it.Count
            list.clear()
            list.addAll(it.List)
            adapter.setNewData(list)
            swipe_layout.finishRefresh()
            adapter.setEnableLoadMore(true)
            adapter.loadMoreComplete()
        })
        viewModel.mHastenCall.observe(this, Observer {
            when (it) {
                0 ->  MyToast.showToast("催交作业成功")
                1 ->  MyToast.showToast("催交作业失败")
                else -> initPaper()
            }
        })
    }

    override fun onResume() {
        super.onResume()
        initPaper()
    }


    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        val homeWork = adapter!!.data[position] as PaperBaseInfo
        when (view?.id) {
            R.id.iv_hastencall -> viewModel.doHastenCallOneOnOne(CurrentUser.Token, CurrentUser.UserID,
                    stuID, homeWork.PaperID, Ext.SCHOOLID)
            R.id.card -> {
                AibkPaperPreviewActivity.launch(this, homeWork.PaperID,"", true)
            }
            R.id.tv_comment -> {
                AibkQuesMarkActivity.launch(this, "setId", stuID, homeWork.PaperID, homeWork.PaperName, true)
            }
            R.id.tv_detail -> {
                AibkAnalysisAnswerSheetActivity_1v1.launch(this, stuID, homeWork.PaperID,
                        homeWork.PaperName)
            }
            R.id.iv_publish -> {
                MyToast.showToast("333333")
            }
        }
    }
}